﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace PicturePuzzle
{
    public class PuzzlePiece
    {

        public Vector2 position = Vector2.Zero;
        
        public Texture2D texture;
        public Rectangle destinationRect, sourceRect;

        public Vector2 velocity = Vector2.Zero;

        public float friction = 0.9f;


        public PuzzlePiece() { }

        public PuzzlePiece(Texture2D texture, Rectangle sourceRect){
            this.texture = texture;
            this.sourceRect = sourceRect;
            this.destinationRect = sourceRect;
            position.X = destinationRect.X;
            position.Y = destinationRect.Y;
        }

        public void Update(GameTimerEventArgs e)
        {
            if (destinationRect.X <= 0 || destinationRect.X >= texture.Bounds.Width - destinationRect.Width) velocity.X *= -1;
            if (destinationRect.Y <= 0 || destinationRect.Y >= texture.Bounds.Height - destinationRect.Height) velocity.Y *= -1;

            position.X += (int) ((e.ElapsedTime.Milliseconds / 100f) * velocity.X);
            position.Y += (int) ((e.ElapsedTime.Milliseconds / 100f) * velocity.Y);

            position.X = MathHelper.Clamp(position.X, 0, 480);
            position.Y = MathHelper.Clamp(position.Y, 0, 800);

            velocity *= friction;
        }

        public void Draw(SpriteBatch batch)
        {
            destinationRect = new Rectangle((int)position.X, (int)position.Y, destinationRect.Width, destinationRect.Height);
            batch.Draw(texture,destinationRect,sourceRect,Color.White);
        }

    }
}
